﻿@using ZhouYu.NewWorld.Model
@{
    Layout = null;
    var parentCategory = ViewBag.categories as List<Category>;
    var count = ViewBag.count;
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <style>
        .right {
            display: none
        }
    </style>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="renderer" content="webkit|ie-comp|ie-stand" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta http-equiv="Cache-Control" content="no-siteapp" />
    <link href="~/assets/css/bootstrap.min.css" rel="stylesheet" />
    <link rel="stylesheet" href="~/css/style.css" />
    <link href="~/assets/css/codemirror.css" rel="stylesheet" />
    <link rel="stylesheet" href="~/assets/css/ace.min.css" />
    <link rel="stylesheet" href="~/font/css/font-awesome.min.css" />
    <link rel="stylesheet" href="~/layui-v2.5.4/layui/css/layui.css" />
    <link rel="stylesheet" href="~/layui-v2.5.4/layui/css/modules/layer/default/layer.css" />
    <link rel="stylesheet" href="~/static/admin/css/style.css" />

    <!--[if lte IE 8]>
      <link rel="stylesheet" href="assets/css/ace-ie.min.css" />
    <![endif]-->
    <script src="~/static/common/jquery-3.3.1.min.js"></script>
    <script src="~/assets/js/bootstrap.min.js"></script>
    <script src="~/assets/js/typeahead-bs2.min.js"></script>
    <script src="~/assets/js/jquery.dataTables.min.js"></script>
    <script src="~/assets/js/jquery.dataTables.bootstrap.js"></script>
    <script src="~/layui-v2.5.4/layui/layui.js" type="text/javascript"></script>
    <script src="~/assets/layer/layer.js" type="text/javascript"></script>
    <script src="~/assets/laydate/laydate.js" type="text/javascript"></script>
    <script src="~/layui-v2.5.4/layui/lay/modules/layer.js"></script>
    <script src="~/Scripts/Format.js"></script>
    <script src="~/static/common/vue.min.js"></script>
    <script src="~/static/admin/js/config.js"></script>
    <script src="~/static/admin/js/script.js"></script>
    <script src="~/layui-v2.5.4/layui/lay/modules/laytpl.js"></script>

    <title>地区特产管理</title>
</head>

<body>
    <div class="margin clearfix">
        <div class="" id="Other_Management">
            <div class="search_style">

                <ul class="search_content clearfix">
                    <li><label class="l_f">地区名称</label><input name="" type="text" id="text" class="text_add" placeholder="请输入地区名称" style=" width:400px" /></li>
                    <li style="width:90px;"><button type="button" id="btn" class="btn_search"><i class="fa fa-search"></i>查询</button></li>
                </ul>
            </div>
            <div class="border clearfix">
                <span class="l_f">
                    <a href="#" class="btn btn-danger" id="add">&nbsp;添加</a>
                </span>
                <span class="l_f">
                    <button class="btn btn-danger" id="batchDelete"><i class="fa fa-trash"></i>&nbsp;批量删除</button>
                </span>
                <span class="r_f">共：<b>@count</b>条</span>
            </div>
            <div class="list_style">
                <table class="layui-hide" id="test" lay-filter="demo"></table>
                <script type="text/html" id="barDemo">
                    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
                    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
                </script>
            </div>
        </div>
    </div>

    @*添加/编辑*@
    <div class="right" id="app">
        <fieldset class="layui-elem-field layui-field-title">
            <legend></legend>
        </fieldset>
        <div class="layui-form">
            <div class="layui-form-item">
                <input type="hidden" name="CategoryId" id="CategoryId" class="layui-input" />
                <label class="layui-form-label">分类名称</label>
                <div class="layui-input-block">
                    <input type="text" name="CategoryName" id="CategoryName" required=required lay-verify="required" placeholder="" autocomplete="off" class="layui-input" />
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">父级ID</label>
            <div class="layui-input-block">
                <select name="ParentId" id="ParentId" style="margin-left:10px">
                    <option value="0" selected=selected>顶级</option>
                    @foreach (var item in parentCategory)
                    {
                        <option value="@item.CategoryId">@item.CategoryName</option>
                    }
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">分类层级</label>
            <div class="layui-input-block">
                <select id="CategoryLevel" name="CategoryLevel" style="margin-left:10px">
                    <option value="1" selected=selected>一级</option>
                    <option value="2" selected=selected>二级</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <input type="button" id="commit" value="立即提交" class="layui-btn" />
            </div>
        </div>
    </div>
</body>
</html>
<script>
    //列表刷新
    function TableReload() {
        var table = layui.table;
        var name = $("#text").val();
        //上述方法等价于
        table.reload('testReload', {
            where: { //设定异步数据接口的额外参数，任意设
                categoryName: name
            }
        });
    }

    //查询按钮点击事件
    $("#btn").click(function () {
        //调用列表刷新方法
        TableReload();
    });

        //显示
        $(function () {
            layui.use('table', function () {
                var table = layui.table;
                table.render({
                    elem: '#test'
                    , url: '/Category/QueryCategory'
                    , cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
                    , cols: [[
                        { type: 'checkbox' } //开启复选框
                        , { field: 'CategoryId', title: '分类ID', sort: true }
                        , { field: 'CategoryName', title: '分类名称', sort: true }
                        , { field: 'ParentId', title: '父分类ID ', sort: true }
                        , { field: 'CategoryLevel', title: '分类层级', sort: true }
                        , { field: 'UpdateTime', title: '修改时间', sort: true, templet: "<div>{{Format(d.UpdateTime ,'yyyy-MM-dd hh:mm:ss')}}</div>" }
                        , { fixed: 'right', title: '操作', toolbar: '#barDemo', width: 150 }
                    ]]
                    , page: true
                    , id: 'testReload'

                });
                //监听行工具事件(编辑/删除)
                table.on('tool(demo)', function (obj) {
                    var data = obj.data;
                    if (obj.event == 'edit') {
                        layer.open({
                            type: 1,
                            title: '修改信息',
                            area: ['800px', '500'],
                            shadeClose: false,
                            content: $("#app"),
                        });
                        $("#CategoryId").val(data.CategoryId);
                        $("#CategoryName").val(data.CategoryName);
                        $("#ParentId").val(data.ParentId);
                        $("#CategoryLevel").val(data.CategoryLevel);

                        $("#commit").click(function () {
                            var category = {};
                            category.CategoryId = $("#CategoryId").val();
                            category.CategoryName = $("#CategoryName").val();
                            category.ParentId = $("#ParentId").val();
                            category.CategoryLevel = $("#CategoryLevel").val();

                            $.ajax({
                                type: "post",
                                data: category,
                                url: "/Category/UpdateCategory",
                                success: function (result) {
                                    if (result.Success) {
                                        layer.msg('修改成功！', {
                                            title: '提示框',
                                            icon: 1,
                                            time: 2000
                                        }
                                            , function () { window.location.reload() });
                                    } else {
                                        layer.msg('修改失败！', {
                                            title: '提示框',
                                            icon: 1,
                                            time: 2000
                                        });
                                    }
                                }
                            });
                        });
                    } else if (obj.event == 'del') {
                        layer.confirm('真的删除行么', function (index) {
                            $.ajax({
                                url: "/Category/DeleteCategory?id=" + data.CategoryId,
                                type: "post",
                                success: function (data) {
                                    if (data.Success) {
                                        layer.msg('删除成功！', {
                                            title: '提示框',
                                            icon: 1,
                                            time: 2000
                                        }, function () {
                                            TableReload();
                                            layer.close(index);
                                        });
                                        window.location.reload();
                                    }
                                    else {
                                        layer.msg('删除失败！', {
                                            title: '提示框',
                                            icon: 1,
                                            time: 2000
                                        });
                                    }
                                }
                            });
                        });
                    }
                });


                //批量删除
                $("#batchDelete").click(function () {
                    //获取复选框集合数据
                    var checkStatus = table.checkStatus('testReload');
                    var data = checkStatus.data;
                    //获得where限制条件Id
                    var codeId = "";
                    if (data.length == 0) {
                        layer.msg('请选择一行', {
                            title: '提示框',
                            icon: 0,
                            time: 2000

                        });
                        return;
                    };
                    //遍历集合,拼接字符串
                    for (var i in data) {
                        if (i == data.length - 1) {
                            codeId += data[i].CategoryId
                        } else {
                            codeId += data[i].CategoryId + ",";

                        }
                    };
                    //温馨提示是否要删除
                    $.ajax({
                        url: "/Category/BatchDelete?Id=" + codeId,
                        type: "post",
                        success: function (operate) {
                            if (operate.Success) {

                                layer.msg('删除成功!！', {
                                    title: '提示框',
                                    icon: 1,
                                    time: 400
                                })
                                location.reload();//刷新页面
                            }
                            else {
                                layer.msg('删除失败！', {
                                    title: '提示框',
                                    icon: 1,
                                    time: 400
                                })
                            }
                        }
                    })
                })
            });

            //添加
            $("#add").click(function () {
                layer.open({
                    type: 1,
                    title: "添加",
                    area: ['800px', '500'],
                    shadeClose: false,
                    content: $("#app"),
                });
                $("#CategoryName").val(null);
                $("#ParentId").val(null);
                $("#CategoryLevel").val(null);

                $("#commit").click(function () {
                    if ($("#CategoryName").val() != null && $("#CategoryName").val() != "" && $("#ParentId").val() != null && $("#ParentId").val() != "" && $("#CategoryLevel").val() != null && $("#CategoryLevel").val() != "") {
                        var category = {};
                        category.CategoryName = $("#CategoryName").val();
                        category.ParentId = $("#ParentId").val();
                        category.CategoryLevel = $("#CategoryLevel").val();

                        $.ajax({
                            type: "post",
                            data: category,
                            url: "/Category/AddCategory",
                            success: function (result) {
                                if (result.Success) {
                                    layer.msg('添加成功！', {
                                        title: '提示框',
                                        icon: 1,
                                        time: 2000
                                    }
                                        , function () { window.location.reload() });
                                } else {
                                    layer.msg('添加失败！', {
                                        title: '提示框',
                                        icon: 1,
                                        time: 2000
                                    });
                                }
                            },
                        });
                    }
                    else {
                        layer.msg('不能为空！', {
                            title: '提示框',
                            icon: 1,
                            time: 2000
                        });
                    }
                });
            }); 

        });
</script>

<script>
    jQuery(function ($) {
        var oTable1 = $('#sample-table').dataTable({
            "aaSorting": [[1, "desc"]],//默认第几个排序
            "bStateSave": true,//状态保存
            "aoColumnDefs": [
                //{"bVisible": false, "aTargets": [ 3 ]} //控制列的隐藏显示
                { "orderable": false, "aTargets": [0, 2, 3, 6] }// 制定列不参与排序
            ]
        });


        $('table th input:checkbox').on('click', function () {
            var that = this;
            $(this).closest('table').find('tr > td:first-child input:checkbox')
                .each(function () {
                    this.checked = that.checked;
                    $(this).closest('tr').toggleClass('selected');
                });

        });
    });
    /*用户-删除*/
    function member_del(obj, id) {
        layer.confirm('确认要删除吗？', function (index) {
            $(obj).parents("tr").remove();
            layer.msg('已删除!', { icon: 1, time: 1000 });
        });
    }
</script>

